/* src/app.js */

// Styles

let tv = require("../tvlib/charting_library/charting_library.min.js");

import config from "../config";

import { PanelStatusWidget, PanelHeader } from "../TlTvApi";

import { environment } from "../../../../environments/environment";

const ins = {
  opts: {},
  widget: null,
  panelStatus: null,
  panelWidget: null,
  init(opts) {
    this.destory();
    this.opts = Object.assign(
      {
        id: "tv_chart_container",
        debug: false,
        library_path: null,
        isLight: true,
        symbol: "",
        url: `${environment.proto}${environment.host.napi}`,
        locale: "",
        skService: null
      },
      opts
    );
    config.init(this.opts);
    this.initChart(this.opts);
  },
  initChart(opts) {
    this.widget = new tv.widget(config.widgetOpts);
    this.widget.onChartReady(() => {
      // this.panelStatus = new PanelStatusWidget({ widget: this.widget });
      this.panelWidget = new PanelHeader({
        widget: this.widget,
        opts: this.opts
      });
    });
  },
  setSymbol({ symbol }) {
    config.widgetOpts.datafeed._requester.setSymbol(symbol);
    this.widget.activeChart().resetData();
    this.widget.activeChart().setSymbol(symbol);
  },
  // 是否已经初始化
  hasIns() {
    return !!this.widget;
  },
  destory() {
    try {
      this.widget.destory();
      this.opts = {};
      this.widget = null;
    } catch (e) {
      this.opts = {};
      this.widget = null;
    }
  }
};

export default ins;
